//
//  LastUpdatetime.m
//  AffairsThrough
//
//  Created by wu jingxing on 12-8-28.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import "XMSLastUpdatetime.h"

NSString *const XMSLastUpdateTimeFileExt = @"lastupdatetime.plist";

NSString *const XMSLastUpdateTimeFile = @"lastupdatetime";

NSString *const XMSLastUpdateTimeDefaultDateTime = @"2000-01-01 00:00:00";

@implementation XMSLastUpdateTime

static NSString *s_szPath;

+ (void)storePath:(NSString *)szPath
{
    [s_szPath release_mm];
    s_szPath = [szPath retain_mm];
}

+ (NSDate*)defaultLastUpdateTime
{
    return [XMSLastUpdateTimeDefaultDateTime toNSDate:NSDateFormatByyMdHms];
}

+ (NSMutableDictionary *)loadLastUpdateTime
{
    NSMutableDictionary *d = nil;
    @synchronized(@"lastUpdateTime")
    {
        NSString *s = [NSString stringWithFormat:@"%@/%@", s_szPath, XMSLastUpdateTimeFileExt];
        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:s];
        d = [[NSMutableDictionary new] autorelease_mm];
        [d setDictionary:dic];
    }
    return d;
}

+ (void)saveLastUpdateTime:(NSMutableDictionary *)dic
{
    @synchronized(@"lastUpdateTime")
    {
        NSString *s = [NSString stringWithFormat:@"%@/%@", s_szPath, XMSLastUpdateTimeFileExt];
        [dic writeToFile:s atomically:NO];
    }
}

+ (NSDate *)getUpdateTime:(NSString *)szKey
{
    NSString *s = [[XMSLastUpdateTime loadLastUpdateTime] objectForKey:szKey];
    return [NSDate formatNSStringDate:s format:yMdHms];
}

+ (void)setUpdateTime:(NSString *)szKey dateTime:(NSDate *)lastupdatetime
{
    NSMutableDictionary *dic = [XMSLastUpdateTime loadLastUpdateTime];
    [dic setObject:[NSDate formatNSDate:lastupdatetime format:yMdHms] forKey:szKey];
    [XMSLastUpdateTime saveLastUpdateTime:dic];
}

+ (void)deleteUpdateTime:(NSString *)szKey
{
    NSMutableDictionary *dic = [XMSLastUpdateTime loadLastUpdateTime];
    [dic removeObjectForKey:szKey];
    [XMSLastUpdateTime saveLastUpdateTime:dic];
}
@end
